#_*_ coding: utf-8 _*_

import serial
import unittest
import binascii
import time
import string


HEAD = '55aa'
cmdQueryWifi =      hex(32) #0x20
cmdConnectWifi =    hex(1)  #0x01
cmdQueryServer =    hex(34) #0x22
cmdSetServer =      hex(5)  #0x05
cmdLogIn =          hex(3)  #0x03
cmdSubscribe =      hex(6)  #0x06
cmdModuleConnect =  hex(18) #0x12
cmdPublish =        hex(7)  #0x07
cmdReset =          hex(48) #0x30

recvQueryWifi =     '55aa0001a000a0'
recvConnectWifi =   '55aa0001810081'
recvQueryServer =   '55aa0001a200a2'
recvSetServer =     '55aa0001850085'
recvLogIn =         '55aa0001830083'
recvSubscribe =     '55aa0001860086'
recvModuleConnect = '55aa0001920092'
recvPublish =       '55aa0001870087'
recvReset =         '55aa0001b000b0'


def mallocPacket(*cmd):
    for n in cmd:
        print n
        if(n == cmdConnectWifi):
            data  = '\x55\xAA\x00\x11'
            data += '\x01'
            data += '\x72\x64\x00\x68\x69\x64\x66\x72\x6F\x62\x6F\x74\x35\x33\x35\x33\x00\x78'
        elif(n == cmdQueryWifi):
            data = '\x55\xAA\x00\x00'
            data+= '\x20'
            data+= '\x1F'
        elif(n == cmdQueryServer):
            data = '\x55\xAA\x00\x00'
            data+= '\x22'
            data+= '\x21'
        elif(n == cmdSetServer):
            data = '\x55\xAA\x00\x13'
            data+= '\x05'
            data+= '\x31\x38\x32\x2e\x32\x35\x34\x2e\x31\x33\x30\x2e\x31\x38\x30\x00\x5B\x07\x00\xFF'
        elif(n == cmdLogIn):
            data = '\x55\xAA\x00\x0A'
            data+= '\x03'
            data+= '\x74\x65\x73\x74\x00\x74\x65\x73\x74\x00\xFF'
        elif(n == cmdSubscribe):
            data = '\x55\xAA\x00\x07'
            data+= '\x06'
            data+= '\x52\x65\x6c\x61\x79\x31\x00\xFF'
        elif(n == cmdModuleConnect):
            data = '\x55\xAA\x00\x00'
            data+= '\x12'
            data+= '\xFF'
        elif(n == cmdPublish):
            data = '\x55\xAA\x00\x0B'
            data+= '\x07'
            data+= '\x52\x65\x6C\x61\x79\x31\x00\x61\x62\x63\x00\xFF'
        elif(n == cmdReset):
            data = '\x55\xAA\x00\x00'
            data+= '\x30'
            data+= '\xFF'


            
    print str(binascii.b2a_hex(data))[0:len(data)*2]
    return data


def atcmd(cmd):
    senddata = mallocPacket(cmd)
    #print repr(senddata)
    com.write(senddata)
    
    while(1):
        if(cmd == cmdConnectWifi):
            time.sleep(8)
        if(cmd == cmdModuleConnect):
            time.sleep(5)
        if(cmd == cmdPublish):
            time.sleep(2)
        num = com.inWaiting()
        print 'serial have date',num
        #print num
        if(num>0):
            data = com.readline()
            data = str(binascii.b2a_hex(data))[0:len(data)*2]#转化为字符串
            print 'recv data:',data
            if(data.count(HEAD)>1):#判断字符串中包含多少个'55aa'
                listdata = data.split(HEAD)
                #print listdata
                for i in listdata:
                    if(i==''):#字符串截取出来第一个为空
                        continue
                    if(i[4:6]=='81'):
                        print 'cmdConnectWifi ok'
                        data=''
                        data+=HEAD
                        data+=i
                        break
                    elif(i[4:6]=='a0'):
                        print 'cmdQueryWifi ok'
                        data=''
                        data+=HEAD
                        data+=i
                        break
                    elif(i[4:6]=='a2'):
                        print 'cmdQueryServer ok'
                        data=''
                        data+=HEAD
                        data+=i
                        break
                    elif(i[4:6]=='85'):
                        print 'cmdSetServer ok'
                        data=''
                        data+=HEAD
                        data+=i
                        break
                    elif(i[4:6]=='83'):
                        print 'cmdLogIn ok'
                        data=''
                        data+=HEAD
                        data+=i
                        break
                    elif(i[4:6]=='86'):
                        print 'cmdSubscribe ok'
                        data=''
                        data+=HEAD
                        data+=i
                        break
                    elif(i[4:6]=='92'):
                        print 'cmdModuleConnect ok'
                        data=''
                        data+=HEAD
                        data+=i
                        break
                    elif(i[4:6]=='87'):
                        print 'cmdPublish ok'
                        data=''
                        data+=HEAD
                        data+=i
                        break
                    elif(i[4:6]=='b0'):
                        print 'cmdReset ok'
                        data=''
                        data+=HEAD
                        data+=i
                        break
                    elif(i[4:6]=='10'):
                        print 'event'    
            if(data[0:4] == HEAD):
                break
        time.sleep(1)
        
    return data



class BLunoTestCls(unittest.TestCase):
    def runTest(self):
        self.assertEqual(recvConnectWifi,atcmd(cmdConnectWifi));
        self.assertEqual(recvQueryWifi,atcmd(cmdQueryWifi));
        
        self.assertEqual(recvSetServer,atcmd(cmdSetServer));
        self.assertEqual(recvLogIn,atcmd(cmdLogIn));
        self.assertEqual(recvSubscribe,atcmd(cmdSubscribe));
        self.assertEqual(recvModuleConnect,atcmd(cmdModuleConnect));
        self.assertEqual(recvQueryServer,atcmd(cmdQueryServer));
        self.assertEqual(recvPublish,atcmd(cmdPublish));
        self.assertEqual(recvReset,atcmd(cmdReset));
        
        return
try:
    com=serial.Serial('com19',38400,timeout=1)
    unittest.main(exit=False);
finally:
    com.close()

